﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using System.Windows.Forms;
using MixScript.DBService;
using System.Diagnostics;

namespace MixScript.BLL.ApplConfig
{
    public struct stAPPLCONFIG
    {
        public string setkey;
        public string setgroup;
        public string setsubgroup;
        public string setval;
        public string setdesc;
    }

    public class cApplConfig : cDBService
    {

        private static List<stAPPLCONFIG> _stApplConfigDB;
        private const string ConfigTable = "sy_appset";
        private const string ConfigStatusTable = "sy_statuslevel";
        private static bool binited;
        private string sSQL = "";
        private int _generalprocid;

        public int GeneralProcIDLvl
        {
            get { return _generalprocid; }
            set { _generalprocid = value; }
        }


             public List<stAPPLCONFIG> stApplConfigDB
        {
            get { return _stApplConfigDB; }
            set { _stApplConfigDB = value; }
        }
        public cApplConfig()
        {
            if (!binited)
            {
               // stApplConfigDB = new List<stAPPLCONFIG>();
                initAppConfigDB();
            }
        }


        private void initAppConfigDB()
        {
            try
            {              
                GeneralProcIDLvl = Convert.ToInt16(getConfigValue("GLOBAL PROCID LEVEL", "", ""));
                binited = true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public bool getAppConfigFromDB(ref DataTable dt, string setkey, string setgroup, string setsubgroup)
        {
            try
            {
                dt= new DataTable();
                dt=getAppConfigFromDB(setkey,setgroup,setsubgroup);
                if(dt.Rows.Count>0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public DataTable getAppConfigFromDB(string setkey, string setgroup, string setsubgroup)
        {
            try
            {
                sSQL = "select * from " + ConfigTable + " where 1=1 ";
                //DataTable dt=new DataTable();
                if (setkey.Trim() != string.Empty)
                {
                    sSQL += "and setkey='" + setkey + "' ";
                }
                if (setgroup.Trim() != string.Empty)
                {
                    sSQL += "and setgroup='" + setgroup + "' ";
                }
                if (setsubgroup.Trim() != string.Empty)
                {
                    sSQL += "and setsubgroup='" + setsubgroup + "' ";
                }

                return Dbserver.ExecuteQuery(sSQL, "APPSET");

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string getConfigValue(string setkey, string setgroup, string setsubgroup)
        {
            try
            {
                DataTable dt=new DataTable();
                if (getAppConfigFromDB(ref dt, setkey, setsubgroup, setsubgroup))
                {
                    return dt.Rows[0]["setval"].ToString();
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public string getLevelProcStatus(string modulename, string procid, int lvl)
        {
            try
            {
                sSQL = "select * from " + ConfigStatusTable + " where 1=1 ";
                if (modulename.Trim() != string.Empty)
                {
                    sSQL += "and modulename='" + modulename + "' ";
                }
                if (procid.Trim() != string.Empty)
                {
                    sSQL += "and procid='" + procid + "' ";
                }
                if (lvl >= 0)
                {
                    sSQL += "and lvl=" + lvl.ToString() + " ";
                }
                DataTable dt = new DataTable();
                dt = Dbserver.ExecuteQuery(sSQL, "sf");
                if (dt.Rows.Count > 0)
                {
                    Debug.WriteLine(dt.Rows[0]["statusval"].ToString());
                    return dt.Rows[0]["statusval"].ToString();
                }
                else
                {
                    Debug.WriteLine("getLevelProcStatus-> status Not Found");
                    return "";
                   

                }
                //return getProcidLevel(modulename, procid, lvl).ToString();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        
        
        public int getProcidLevel(string modulename, string procid,int lvl)
        {
            try
            {
                sSQL = "select * from " + ConfigStatusTable + " where 1=1 ";
                if (modulename.Trim() != string.Empty)
                {
                    sSQL += "and modulename='" + modulename + "' ";
                }
                if (procid.Trim() != string.Empty)
                {
                    sSQL += "and procid='" + procid + "' ";
                }
                if (lvl >=0)
                {
                    sSQL += "and lvl=" + lvl.ToString() + " ";
                }
                DataTable dt= new DataTable();
                dt = Dbserver.ExecuteQuery(sSQL, "sf");
                if (dt.Rows.Count > 0)
                {
                    Debug.WriteLine("getProcidLevel->" + dt.Rows[0]["lvl"].ToString());
                     return Convert.ToInt16( dt.Rows[0]["lvl"]);
                    
                }
                else
                {
                    Debug.WriteLine("getProcidLevel->Not Found -1");
                    return -1;

                }


            }
            catch (Exception ex)
            {

                throw;
            }
        }
        




    }
  
}
